package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xmy.cultivate.entity.StudentForData;
import com.xmy.cultivate.mapper.StudentForDataMapper;
import com.xmy.cultivate.service.IStudentForDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IStudentForDataTypeService;
import com.xmy.cultivate.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 不同类型，需要排除或参与的数据，如：人教版学员不参与率的计算，待转校学生 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-01-08
 */
@Service
public class StudentForDataServiceImpl extends ServiceImpl<StudentForDataMapper, StudentForData> implements IStudentForDataService {

    @Autowired
    @Lazy
    StudentForDataMapper studentForDataMapper;

    @Autowired
    @Lazy
    IStudentForDataTypeService iStudentForDataTypeService;

    @Override
    public IPage<StudentForData> findAll(IPage<StudentForData> page, Long schoolId, Long studentId, Long subjectsId, Integer yearPart, Integer quarterNum, Integer lessonType, Long type, Integer yearClassId, Long gradeId, Long teacherId, String studentName) {
        IPage<StudentForData> studentWaitingTransferSchooolIPage = studentForDataMapper.findAll(page, schoolId, studentId, subjectsId, yearPart, quarterNum, lessonType, type, yearClassId, gradeId, teacherId, studentName);
        for (StudentForData studentForData : studentWaitingTransferSchooolIPage.getRecords()) {
            if (studentForData.getSubjectsId() != null) {
                studentForData.setSubjectsName(CommonUtil.getSubjectsNameForId(Integer.parseInt(studentForData.getSubjectsId().toString())));
            }
            studentForData.setYearClassName(CommonUtil.getYearClassNameForId(studentForData.getYearClassId()));
            studentForData.setQuarterName(CommonUtil.getQuarterNameForId(studentForData.getQuarterNum()));
        }
        return studentWaitingTransferSchooolIPage;
    }

    /**
     * 获取待添加的转校学员
     *
     * @param page
     * @param schoolId
     * @param studentId
     * @param subjectsId
     * @param yearPart
     * @param quarterNum
     * @param lessonType
     * @param type
     * @param gradeId
     * @param studentName
     * @return
     */
    @Override
    public IPage<StudentForData> getWaitTransterStudent(IPage<StudentForData> page, Long schoolId, Long studentId, Long subjectsId, Integer yearPart, Integer quarterNum, Integer lessonType, Long type,Integer yearClassId,Long gradeId,Long teacherId, String studentName) {
        IPage<StudentForData> studentForDataIPage = studentForDataMapper.getWaitTransterStudent(page, schoolId, studentId, subjectsId, yearPart, quarterNum, lessonType, type, yearClassId,gradeId,teacherId, studentName);
        for (StudentForData studentForData:studentForDataIPage.getRecords()){
            studentForData.setYearClassName(CommonUtil.getYearClassNameForId(studentForData.getYearClassId()));
        }
        return studentForDataIPage;
    }

    @Override
    public boolean updateStatusForWaitTranster(Integer yearPart, Integer quarterNum, Long studentId, Long subjectsId, Integer status) {
        Long typeId = iStudentForDataTypeService.getIdForType(1);
        UpdateWrapper<StudentForData> studentForDataUpdateWrapper = new UpdateWrapper<>();
        studentForDataUpdateWrapper.eq("year_part", yearPart);
        studentForDataUpdateWrapper.eq("quarter_num", quarterNum);
        studentForDataUpdateWrapper.eq("student_id", studentId);
        studentForDataUpdateWrapper.eq("subjects_id", subjectsId);
        studentForDataUpdateWrapper.eq("type_id", typeId);
        studentForDataUpdateWrapper.set("status", status);
        this.update(studentForDataUpdateWrapper);
        return true;
    }
}
